require.config({baseUrl: '/js/lib',urlArgs: "v0.0.3"});
require(['zepto', 'lib'], function($, lib) {
  var invoice = {}, lib = new lib();
  $.extend(invoice, {
    oId: lib.getUrlParam('oId'),
    invoiceInfo: JSON.parse(sessionStorage.invoiceInfo || '{"invoiceType": 0}'),
    init: function(){
      this.bindEvent();
      if(this.invoiceInfo.invoiceType === "2"){
        $(".ui-can-tap:eq(0)").trigger("tap");
        $(".ui-can-tap:eq(0)").children('div').trigger('tap');
        $(".ui-can-tap:eq(0)").children('div').eq(0).children('input').val(this.invoiceInfo.invoiceTitle);
        $(".ui-can-tap:eq(0)").children('div').eq(1).children('input').val(this.invoiceInfo.invoiceEmail);
        $(".ui-can-tap:eq(0)").children('div').eq(2).children('input').val(this.invoiceInfo.invoicePhone);
      }else if(this.invoiceInfo.invoiceType === "1"){
        $(".ui-can-tap:eq(1)").trigger("tap");
        $(".ui-can-tap:eq(1)").children('div').trigger('tap');
        $(".ui-can-tap:eq(1)").children('div').eq(0).children('input').val(this.invoiceInfo.invoiceTitle);
        $(".ui-can-tap:eq(1)").children('div').eq(1).children('input').val(this.invoiceInfo.invoiceTaxpayerId);
        $(".ui-can-tap:eq(1)").children('div').eq(2).children('input').val(this.invoiceInfo.invoiceEmail);
        $(".ui-can-tap:eq(1)").children('div').eq(3).children('input').val(this.invoiceInfo.invoicePhone);
        
      }
    },
    bindEvent: function(){
      var _t = this;
      $(document).on("tap", ".ui-can-tap", function(){
        if($(this).children("i").attr("class") === "ui-up-r"){
          $(this).children("i").attr("class","ui-down-g");
          $(this).children("div").css("display", "none");
        }else{
          $(this).children("i").attr("class", "ui-up-r");
          $(this).children("div").css("display", "block");
        }
      });
      
      $(".ui-can-tap > div").tap(function(e){
        e.stopPropagation();
        $(".ui-choose").attr("class", "ui-no-choose");
        if($(this).children("i").attr("class") === "ui-no-choose"){
          $(this).children("i").attr("class", "ui-choose");
          $(this).siblings().children("i").attr("class", "ui-choose");
        }else{
          $(this).children("i").attr("class", "ui-no-choose");
        }
      });
      
      $(".ui-invoice-info > li:eq(0)").tap(function(e){
        e.stopPropagation();
        $(".ui-choose").attr("class", "ui-no-choose");
        if($(this).children("i").attr("class") === "ui-no-choose"){
          $(this).children("i").attr("class", "ui-choose");
        }else{
          $(this).children("i").attr("class", "ui-no-choose");
        }
      });
      
      $(".ui-red-botton").tap(function(){
        var data = {};
        data.orderId = _t.oId;
        data.invoiceType = $(".ui-choose").attr('data-invoiceType');
        data.invoiceContent = '食品酒水';
        var emailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/ ;
        var phoneReg = /^1[34578]\d{9}$/;
        if(data.invoiceType === "1"){
          if($(".ui-choose").eq(0).prev().val().trim().length === 0){
            lib.showMessage("请输入抬头信息");
            return;
          }
          data.invoiceTitle = $(".ui-choose").eq(0).prev().val();
          if($(".ui-choose").eq(1).prev().val().trim().length === 0){
            lib.showMessage("请输入纳税人识别号");
            return;
          }
          data.invoiceTaxpayerId = $(".ui-choose").eq(1).prev().val();
          if(!emailReg.test($(".ui-choose").eq(2).prev().val())){
            lib.showMessage("请输入正确的邮箱地址");
            return;
          }
          data.invoiceEmail = $(".ui-choose").eq(2).prev().val();
          if(!phoneReg.test($(".ui-choose").eq(3).prev().val())){
            lib.showMessage("请输入正确的手机号");
            return;
          }
          data.invoicePhone = $(".ui-choose").eq(3).prev().val();
        }
        if(data.invoiceType === "2"){
          if($(".ui-choose").eq(0).prev().val().trim().length === 0){
            lib.showMessage("请输入抬头信息");
            return;
          }
          data.invoiceTitle = $(".ui-choose").eq(0).prev().val();
          if(!emailReg.test($(".ui-choose").eq(1).prev().val())){
            lib.showMessage("请输入正确的邮箱地址");
            return;
          }
          data.invoiceEmail = $(".ui-choose").eq(1).prev().val();
          if(!phoneReg.test($(".ui-choose").eq(2).prev().val())){
            lib.showMessage("请输入正确的手机号");
            return;
          }
          data.invoicePhone = $(".ui-choose").eq(2).prev().val();
        }
        if(!_t.oId){
          sessionStorage.invoiceInfo = JSON.stringify(data);
          lib.getUrlParam('backUrl') ? location.href = lib.getUrlParam('backUrl') : '';
          return;
        }
        lib.ajx(lib.getReq().ser + '/order/updateOrder.action', data, function(data){
          if(data.infocode === "0"){
            lib.getUrlParam('backUrl') ? location.href = lib.getUrlParam('backUrl') : '';
            return;
          }
          lib.showMessage(data.info);
        }, function(){
          lib.showMessage("网络延迟， 请稍后再试");
        });
      });
    },
  });
  invoice.init();
});